#include "player.hpp"

using namespace std;

player::player()
{
    x = 10;
    y = 10;
    vx = 0;
    vy = 0;
    ax = 0;
    ay = .05;
    pic = load_image("player.bmp", false);
    falling = false;
    jumping = false;
    left = false;
    right = false;
    friction = .05;
}

player::player(int nx, int ny)
{
    x = nx;
    y = ny;
    vx = 0;
    vy = 0;
    ax = 0;
    ay = .05;
    pic = load_image("player.bmp");
    falling = false;
    jumping = false;
    left = false;
    right = false;
    friction = .05;
}

void player::move(bool u, bool d, bool l, bool r)
{
    hMove(l, r);
    vMove(u, d);
}
void player::hMove(bool l, bool r)
{
    if(l) //running
    {
        ax = -.1;
        right = false;
        left = true;
    }
    if(r) //running
    {
        ax = .1;
        right = true;
        left = false;
    }
    if(!r && !l) //Not running
    {
        ax = 0;
    }

    if(right) //friction MOVING right
    {
        ax -= friction;
        if(vx < 0)
        {
            right = false;
            vx = 0;
            ax = 0;
        }

    }
    if(left) //friction MOVING left
    {
        ax += friction;
        if(vx > 0)
        {
            left = false;
            vx = 0;
            ax = 0;
        }
    }
    vx += ax;
    if(vx > 3)
        vx = 3;
    if(vx < -3)
        vx = -3;
    x += vx;
}
void player::vMove(bool u, bool d)
{
    /*if(u && !jumping)
    {
        jumping = true;
        vy = -2;
    }
    vy += ay;
    if(y + vy > 460)
    {
        y = 419;
        jumping = false;
    }
    else
        y += vy;*/
    if(falling)
    {
        ay = .1;
    }
    if(u && !jumping)
    {
        vy = -5;
        jumping = true;
    }
    vy += ay;
    if(falling || u)
    {
        y += vy;
        if(y + 40 > 420 && !u)
            y = 419;
        jumping = false;
    }
}
void player::draw(SDL_Surface *screen)
{
    apply_surface(x, y, pic, screen);
}
